﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoPersona : Form
    {
        [Dependency]
        public IPersonaBL personaBL { get; set; }

        [Dependency]
        public ISexoBL sexoBL { get; set; }

        [Dependency]
        public IEstadoCivilBL estadoCivilBL { get; set; } 

        public FrmMttoPersona()
        {
            InitializeComponent();
            this.BuildUp();

            sEXOBindingSource.DataSource = sexoBL.GetAllFromSexo();
            eSTADOCIVILBindingSource.DataSource = estadoCivilBL.GetAllFromEstadoCivil();
        }

        bool isNew;

        public void NuevaPersona()
        {
            isNew = true;
            pERSONABindingSource.AddNew();
            
        }

        public void NuevaPersonaDH(string dni)
        {
            isNew = true;
            pERSONABindingSource.AddNew();
            var persona = pERSONABindingSource.Current as PERSONA;
            persona.Dni = dni;
        }
        public PERSONA _persona { get; set; }
        public DERECHO_HABIENTE _derecho { get; set; }

        public void EditarPersona()
        {
            isNew = false;
            pERSONABindingSource.DataSource = _persona;
        }

        private void FrmMttoPersona_Load(object sender, EventArgs e)
        {

        }

        private void cbEstadoCivil_Click(object sender, EventArgs e)
        {
            eSTADOCIVILBindingSource.DataSource = estadoCivilBL.GetAllFromEstadoCivil();
        }

        private void cbEstadoCivil_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbEstadoCivil.SelectedValue != null)
            {
                idEstadoCivilTextBox.Text = cbEstadoCivil.ToString();
            }
        }

        private void cbSexo_Click(object sender, EventArgs e)
        {
            sEXOBindingSource.DataSource = sexoBL.GetAllFromSexo();
        }

        private void cbSexo_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbSexo.SelectedValue != null)
            {
                idSexoTextBox.Text = cbSexo.ToString();
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var persona = pERSONABindingSource.Current as PERSONA;
            var msg =   "ERROR";

            if (persona != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        persona.ApeMaterno = apeMaternoTextBox.Text.ToUpper();
                        persona.ApePaterno = apePaternoTextBox.Text.ToUpper();
                        persona.Nombres = nombresTextBox.Text.ToUpper();
                        persona.Direccion = direccionTextBox.Text.ToUpper();
                        persona.FechaNacimiento = fechaNacimientoDateTimePicker.Value;
                        

                        personaBL.AddPersona(persona);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        persona.ApeMaterno = apeMaternoTextBox.Text.ToUpper();
                        persona.ApePaterno = apePaternoTextBox.Text.ToUpper();
                        persona.Nombres = nombresTextBox.Text.ToUpper();
                        persona.Direccion = direccionTextBox.Text.ToUpper();

                        var sexo = (from c in sexoBL.GetAllFromSexo()
                                    where c.IdSexo.Equals(persona.IdSexo)
                                    select c).SingleOrDefault();
                        var estCiv = (from c in estadoCivilBL.GetAllFromEstadoCivil()
                                      where c.IdEstadoCivil.Equals(persona.IdEstadoCivil)
                                      select c).SingleOrDefault();
                        persona.SEXO = sexo;
                        persona.ESTADO_CIVIL = estCiv;

                        personaBL.UpdatePersona(persona);
                        MessageBox.Show("Modificación Correcta", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void dniTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                existeDni();
            }
        }
        public void existeDni()
        {
            var persona = personaBL.GetFromPersonaByDni(dniTextBox.Text);

            if (dniTextBox.Text.Length != 0)
            {
                if (dniTextBox.Text.Length == 8)
                {
                    if (persona != null)
                    {
                        pERSONABindingSource.DataSource = persona;
                        MessageBox.Show("Persona Existente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                    {
                        apePaternoTextBox.ReadOnly = false;
                        apeMaternoTextBox.ReadOnly = false;
                        nombresTextBox.ReadOnly = false;
                        direccionTextBox.ReadOnly = false;
                        emailTextBox.ReadOnly = false;
                        cbEstadoCivil.Enabled = true;
                        cbSexo.Enabled = true;
                        fechaNacimientoDateTimePicker.Enabled = true;
                    }
                }
                else
                {
                    MessageBox.Show("DNI incorrecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("Ingrese un DNI", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void btnVerificarDni_Click(object sender, EventArgs e)
        {
            existeDni();
        }

        private void dniTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                existeDni();
            }
        }

        private void dniTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apePaternoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apeMaternoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void nombresTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }


    }
}
